package com.taptech.doufu.ui.view.personalcenter;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.taptech.doufu.ui.view.PullToRefreshListView;

public class DynamicListView extends PullToRefreshListView{
	
	private HorizontalMoveListener horizontalMoveListener;


	public void setHorizontalMoveListener(
			HorizontalMoveListener horizontalMoveListener) {
		this.horizontalMoveListener = horizontalMoveListener;
	}

	public DynamicListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	public DynamicListView(Context context, AttributeSet attrs) {
		super(context, attrs); 
	}
	
	public DynamicListView(Context context) {
		super(context);
	}
	
	private float xDistance, yDistance, xLast, yLast;
	@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
         switch (ev.getAction()) {
         case MotionEvent.ACTION_DOWN:
                 xDistance = yDistance = 0f;
                 xLast = ev.getX();
                 yLast = ev.getY();
                 break;
         case MotionEvent.ACTION_MOVE:       	 
        	 float curX = ev.getX();
             float curY = ev.getY();
             float xSpace = curX - xLast;
             xDistance += Math.abs(xSpace);
             yDistance += Math.abs(curY - yLast);
             if(horizontalMoveListener!=null)
            	 horizontalMoveListener.move((int)xSpace);
             if (xDistance > yDistance) {
            	 return false;
             }       	 
        	 break;
                
         case MotionEvent.ACTION_UP:       	 
        	 curX = ev.getX();
        	 curY = ev.getY();
             xSpace = curX - xLast;
             xDistance += Math.abs(xSpace);
             yDistance += Math.abs(curY - yLast);

             
             if (xDistance > yDistance) {
            	 if(horizontalMoveListener!=null)
            	 {
            		 if(xSpace>50)
                	 {
            			 horizontalMoveListener.leftMove();                    		 	
                	 }
            		 else if(xSpace<-50)
            		 {
            			 horizontalMoveListener.rightMove(); 
            		 }
            	 }          	
                 return true;   //表示向下传递事件
             }
        	 
         }
         return super.onInterceptTouchEvent(ev);
	}

	public interface HorizontalMoveListener{
		public void leftMove();
		public void rightMove();
		public void move(int space);
	}
}
